package com.wing.bill.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.service.WeighCabinetReportService;
import com.wing.cabinet.utils.FindDateStatusUtil;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.wcabinet.model.entity.WcabCellInventoryInout;
import com.wing.wcabinet.model.entity.WcabCheckedResult;
import com.wing.wcabinet.model.response.WcabCellInventoryInoutVO;
import com.wing.wcabinet.model.response.WcabCheckedResultVO;
import com.wing.wcabinet.service.WcabCellInventoryInoutService;
import com.wing.wcabinet.service.WcabCheckedResultService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Validated
@Api(value = "Admin-称重柜报表", tags = "#/bill/weighCabinetReport")
@RestController("weighCabinetReportController")
@RequestMapping("bill/admin/weighCabinetReport")
@RequiredArgsConstructor
public class WeighCabinetReportController {

    private final WeighCabinetReportService weighCabinetReportService;

    private final WcabCellInventoryInoutService wcabCellInventoryInoutService;

    private final WcabCheckedResultService wcabCheckedResultService;

    //顶部数据和点击
    @ApiOperation(value = "每个选项卡页面的顶部数据", notes = "查询")
    @GetMapping("/headResult")
    public JsonResult result(
            @RequestParam("type")String type//123 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("click")String click//顶部数据点击
            ,@RequestParam("table")String table//选项卡 从左至右1-5
    ) {
        String code = JwtUtils.getEnterpriseCode();
        if (table.equals("1")){//上货选项卡
            return weighCabinetReportService.exhibitList(type,startTime,endTime,click,code);
        }else if(table.equals("2")){//领新选项卡
            return weighCabinetReportService.stockList(type,startTime,endTime,click,code);
        }
        else if(table.equals("3")){//退料选项卡
            return weighCabinetReportService.returnedList(type,startTime,endTime,click,code);
        }
        else if(table.equals("4")){//报修选项卡
            return weighCabinetReportService.repairList(type,startTime,endTime,click,code);
        }
        else if(table.equals("5")){//报废选项卡
            return weighCabinetReportService.recycleList(type,startTime,endTime,click,code);
        }
        return JsonResult.fail("查询失败");
    }

    @ApiOperation(value = "上货物料排名", notes = "查询")
    @GetMapping("/exhibitRanking")
    public JsonResult exhibitRanking(
            @RequestParam("type")String type//123 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return JsonResult.success(weighCabinetReportService.exhibitRanking(type,startTime,endTime,code));
    }

    @ApiOperation(value = "补货物料排名", notes = "查询")
    @GetMapping("/replenishment")
    public JsonResult findReplenishmentMaterial(
            @RequestParam("type")String type//123 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return JsonResult.success(weighCabinetReportService.findReplenishmentMaterial(type,startTime,endTime,code));
    }

    @ApiOperation(value = "物料上货补货记录列表", notes = "查询")
    @GetMapping("/exhibitRecordList")
    public PageResult<WcabCellInventoryInoutVO> exhibitRecordList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,@RequestParam("type")String type//123 周月年
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
    ){
        Page<WcabCellInventoryInout> pg = new Page<>(page, limit);
        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("enterpriseCode",JwtUtils.getEnterpriseCode());
        if(StringUtils.isNotBlank(type)){
            if(type.equals("1")){
                queryParam.put("startCreateTime", FindDateStatusUtil.dayUtil().get(0));
                queryParam.put("endCreateTime",FindDateStatusUtil.dayUtil().get(1));
            }else if(type.equals("2")){
                queryParam.put("startCreateTime", FindDateStatusUtil.weekUtil().get(0));
                queryParam.put("endCreateTime",FindDateStatusUtil.weekUtil().get(1));
            }else if(type.equals("3")){
                queryParam.put("startCreateTime", FindDateStatusUtil.dateUtil().get(0));
                queryParam.put("endCreateTime",FindDateStatusUtil.dateUtil().get(1));
            }
        }
        if(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            queryParam.put("startCreateTime", startTime);
            queryParam.put("endCreateTime",endTime);
        }
        queryParam.put("operationType",WcabCellInventoryInout.OperationType.supplyIn.getValue());
        IPage<WcabCellInventoryInoutVO> list = wcabCellInventoryInoutService.exhibitRecordList(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "物料消耗量（产品）", notes = "查询")
    @GetMapping("/findMaterialConsumption")
    public JsonResult findMaterialConsumption(
            @RequestParam("type")String type//123 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime  //时间搜索 结束时间
            ,@RequestParam("other")String other//柱状图饼状图的下拉选
            ,@RequestParam("param")String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        String code = JwtUtils.getEnterpriseCode();
        return JsonResult.success(weighCabinetReportService.findMaterialConsumption(type,startTime,endTime,other,param,code));
    }

    @ApiOperation(value = "物料类型", notes = "查询")
    @GetMapping("/findMaterialByType")
    public JsonResult findMaterialByType(
            @RequestParam("type")String type//123 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime  //时间搜索 结束时间
            ,@RequestParam("param")String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        String code = JwtUtils.getEnterpriseCode();
        return JsonResult.success(weighCabinetReportService.findMaterialByType(type,startTime,endTime,param,code));
    }

    @ApiOperation(value = "员工领用统计", notes = "查询")
    @GetMapping("/findRequisitionStatistics")
    public JsonResult findRequisitionStatistics(
            @RequestParam("type")String type//123 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime  //时间搜索 结束时间
            ,@RequestParam("other")String other//柱状图饼状图的下拉选
            ,@RequestParam("param")String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        String code = JwtUtils.getEnterpriseCode();
        return JsonResult.success(weighCabinetReportService.findRequisitionStatistics(type,startTime,endTime,other,param,code));
    }

    @ApiOperation(value = "物料品牌", notes = "查询")
    @GetMapping("/findMaterialConsumeByBrand")
    public JsonResult findMaterialConsumeByBrand(
            @RequestParam("type")String type//123 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime  //时间搜索 结束时间
            ,@RequestParam("param")String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        String code = JwtUtils.getEnterpriseCode();
        return JsonResult.success(weighCabinetReportService.findMaterialConsumeByBrand(type,startTime,endTime,param,code));
    }

    @ApiOperation(value = "物料领新记录列表", notes = "查询")
    @GetMapping("/queryReceiverRecordList")
    public PageResult<WcabCellInventoryInoutVO> queryReceiverRecordList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,@RequestParam("type")String type//123 周月年
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
    ){
        Page<WcabCellInventoryInout> pg = new Page<>(page, limit);
        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("enterpriseCode",JwtUtils.getEnterpriseCode());
        if(StringUtils.isNotBlank(type)){
            if(type.equals("1")){
                queryParam.put("startCreateTime", FindDateStatusUtil.dayUtil().get(0));
                queryParam.put("endCreateTime",FindDateStatusUtil.dayUtil().get(1));
            }else if(type.equals("2")){
                queryParam.put("startCreateTime", FindDateStatusUtil.weekUtil().get(0));
                queryParam.put("endCreateTime",FindDateStatusUtil.weekUtil().get(1));
            }else if(type.equals("3")){
                queryParam.put("startCreateTime", FindDateStatusUtil.dateUtil().get(0));
                queryParam.put("endCreateTime",FindDateStatusUtil.dateUtil().get(1));
            }
        }
        if(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            queryParam.put("startCreateTime", startTime);
            queryParam.put("endCreateTime",endTime);
        }
        queryParam.put("inoutType",WcabCellInventoryInout.InoutType.out.getValue());
        queryParam.put("operationType",WcabCellInventoryInout.OperationType.out.getValue());
        IPage<WcabCellInventoryInoutVO> list = wcabCellInventoryInoutService.exhibitRecordList(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "退料物料占比", notes = "查询")
    @GetMapping("/findProportionOfReturned")
    public JsonResult findProportionOfReturnedMaterials(
            @RequestParam("type")String type//123 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime  //时间搜索 结束时间
            ,@RequestParam("other")String other//柱状图饼状图的下拉选
    ){
        String code = JwtUtils.getEnterpriseCode();
        return JsonResult.success(weighCabinetReportService.findProportionOfReturnedMaterials(type,startTime,endTime,other,code));
    }

    @ApiOperation(value = "退料物料排名", notes = "查询")
    @GetMapping("/findReturnedMaterialRanking")
    public JsonResult findReturnedMaterialRanking(
            @RequestParam("type")String type//123 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime  //时间搜索 结束时间
    ){
        String code = JwtUtils.getEnterpriseCode();
        return JsonResult.success(weighCabinetReportService.findReturnedMaterialRanking(type,startTime,endTime,code));
    }

    @ApiOperation(value = "退料记录列表", notes = "查询")
    @GetMapping("/queryReturnedRecordList")
    public PageResult<WcabCellInventoryInoutVO> queryReturnedRecordList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,@RequestParam("type")String type//123 周月年
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
    ){
        Page<WcabCellInventoryInout> pg = new Page<>(page, limit);
        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("enterpriseCode",JwtUtils.getEnterpriseCode());
        if(StringUtils.isNotBlank(type)){
            if(type.equals("1")){
                queryParam.put("startCreateTime", FindDateStatusUtil.dayUtil().get(0));
                queryParam.put("endCreateTime",FindDateStatusUtil.dayUtil().get(1));
            }else if(type.equals("2")){
                queryParam.put("startCreateTime", FindDateStatusUtil.weekUtil().get(0));
                queryParam.put("endCreateTime",FindDateStatusUtil.weekUtil().get(1));
            }else if(type.equals("3")){
                queryParam.put("startCreateTime", FindDateStatusUtil.dateUtil().get(0));
                queryParam.put("endCreateTime",FindDateStatusUtil.dateUtil().get(1));
            }
        }
        if(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            queryParam.put("startCreateTime", startTime);
            queryParam.put("endCreateTime",endTime);
        }
        queryParam.put("inoutType",WcabCellInventoryInout.InoutType.in.getValue());
        queryParam.put("operationType",WcabCellInventoryInout.OperationType.returnIn.getValue());
        IPage<WcabCellInventoryInoutVO> list = wcabCellInventoryInoutService.exhibitRecordList(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "报修报修报废消耗排名", notes = "查询")
    @GetMapping("/findRepairMaterialBySkuCode")
    public JsonResult findRepairMaterialBySkuCode(
            @RequestParam("type")String type//123 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime  //时间搜索 结束时间
            ,@RequestParam("businessType")String businessType // 报修报废
    ){
        String code = JwtUtils.getEnterpriseCode();
        return JsonResult.success(weighCabinetReportService.findRepairMaterialBySkuCode(type,startTime,endTime,code,Integer.valueOf(businessType)));
    }

    @ApiOperation(value = "按人员查询报修报废物料消耗排名", notes = "查询")
    @GetMapping("/findRepairMaterialByUser")
    public JsonResult findRepairMaterialByUser(
            @RequestParam("type")String type//123 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime  //时间搜索 结束时间
            ,@RequestParam("businessType")String businessType // 报修报废
    ){
        String code = JwtUtils.getEnterpriseCode();
        return JsonResult.success(weighCabinetReportService.findRepairMaterialByUser(type,startTime,endTime,code,Integer.valueOf(businessType)));
    }

    @ApiOperation(value = "按类型查询报修报废物料消耗排名", notes = "查询")
    @GetMapping("/findRepairMaterialByProductType")
    public JsonResult findRepairMaterialByProductType(
            @RequestParam("type")String type//123 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime  //时间搜索 结束时间
            ,@RequestParam("businessType")String businessType // 报修报废
    ){
        String code = JwtUtils.getEnterpriseCode();
        return JsonResult.success(weighCabinetReportService.findRepairMaterialByProductType(type,startTime,endTime,code,Integer.valueOf(businessType)));
    }

    @ApiOperation(value = "报修存入记录列表", notes = "查询")
    @GetMapping("/findRepairRecordListList")
    public PageResult<WcabCellInventoryInoutVO> findRepairRecordListList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,@RequestParam("type")String type//123 周月年
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("businessType")String businessType // 报修报废
    ){
        Page<WcabCheckedResult> pg = new Page<>(page, limit);
        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("enterpriseCode",JwtUtils.getEnterpriseCode());
        if(StringUtils.isNotBlank(type)){
            if(type.equals("1")){
                queryParam.put("startCreateTime", FindDateStatusUtil.dayUtil().get(0));
                queryParam.put("endCreateTime",FindDateStatusUtil.dayUtil().get(1));
            }else if(type.equals("2")){
                queryParam.put("startCreateTime", FindDateStatusUtil.weekUtil().get(0));
                queryParam.put("endCreateTime",FindDateStatusUtil.weekUtil().get(1));
            }else if(type.equals("3")){
                queryParam.put("startCreateTime", FindDateStatusUtil.dateUtil().get(0));
                queryParam.put("endCreateTime",FindDateStatusUtil.dateUtil().get(1));
            }
        }
        if(StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            queryParam.put("startCreateTime", startTime);
            queryParam.put("endCreateTime",endTime);
        }
        if(StringUtils.isNotBlank(businessType)){
            if(businessType.equals("2")){
                queryParam.put("operationType",WcabCellInventoryInout.OperationType.repairIn.getValue());
            }else if(businessType.equals("3")){
                queryParam.put("operationType",WcabCellInventoryInout.OperationType.recycleIn.getValue());
            }
        }
        IPage<WcabCellInventoryInoutVO> list = wcabCellInventoryInoutService.exhibitRecordList(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }
}
